package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.domain.VideoAuth;
import cn.wolfcode.p2p.base.mapper.VideoAuthMapper;
import cn.wolfcode.p2p.base.query.VideoAuthQueryObject;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.util.Assert;
import cn.wolfcode.p2p.util.BitStatesUtils;
import cn.wolfcode.p2p.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

import static cn.wolfcode.p2p.base.domain.BaseAuditDomain.STATE_SUCCESS;

@Service
@Transactional
public class VideoAuthServiceImpl implements IVideoAuthService {

    @Autowired
    private VideoAuthMapper videoAuthMapper;
    @Autowired
    private IUserInfoService userInfoService;


    @Override
    public VideoAuth getById(Long videoAuthId) {
        return videoAuthMapper.selectByPrimaryKey(videoAuthId);
    }

    @Override
    public void apply(VideoAuth videoAuth) {
        //参数校验
        //申请人
        LoginInfo applier = UserContext.getLoginInfo();
        //基本资料对象-->查询整条信息
        UserInfo userInfo = userInfoService.getById(applier.getId());
        //判断是否视频认证通过
        Assert.isFalse(userInfo.hasVedioAuth(), "同学,你的视频认证已通过");
        //判断是否有申请预约
        Assert.isFalse(userInfo.getVideoAuthId() != null, "同学,你的视频认证正在申请中");
        //保存视频的认证记录
        //设值审核客服(前台传过来的)
        //设值预约日期(前台传过来的)
        //设值预约时间(前台传过来的)
        //设置申请人
        //设置申请时间
        VideoAuth va = new VideoAuth();
        va.setApplier(applier);
        va.setAuditor(videoAuth.getAuditor());
        va.setOrderDate(videoAuth.getOrderDate());
        va.setOrderTime(videoAuth.getOrderTime());
        va.setApplyTime(new Date());
        videoAuthMapper.insert(va);

        //把视频对象的id设置给userinfo.videoAuthId
        userInfo.setVideoAuthId(va.getId());
        userInfoService.update(userInfo);

    }

    @Override
    public PageInfo queryForPages(VideoAuthQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List list = videoAuthMapper.selectFoPages(qo);
        return new PageInfo(list);
    }

    @Override
    public void audit(Long id, int state, String remark) {
        //信息校验
        VideoAuth auth = videoAuthMapper.selectByPrimaryKey(id);

        //判断视频对象是否处于待审核转态
        Assert.isFalse(auth.getState() != VideoAuth.STATE_NORMAL, "同学,当前的状态不是待审核");

        //设置审核相关信息
        //审核状态
        auth.setState(state);
        //审核时间
        auth.setAuditTime(new Date());
        //审核备注
        auth.setRemark(remark);
        update(auth);
        //申请人的id 获取user对象
        UserInfo userInfo = userInfoService.getById(auth.getApplier().getId());
        //如果申请成功
        if (state == STATE_SUCCESS) {
            //加上位状态
            userInfo.setBitState(BitStatesUtils.OP_VEDIO_AUTH);

        }else{
            //如果失败,删除videoAuthId
            userInfo.setVideoAuthId(null);
        }
        userInfoService.update(userInfo);


    }
    @Override
    public void update(VideoAuth videoAuth){
        videoAuthMapper.updateByPrimaryKey(videoAuth);
    }
}
